package com.ruoyi.labdanger.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.labdanger.domain.LabApplyPurchase;
import com.ruoyi.labdanger.domain.LabApplyPurchaseDetails;
import com.ruoyi.labdanger.mapper.LabApplyPurchaseMapper;
import com.ruoyi.labdanger.service.ILabApplyPurchaseDetailsService;
import com.ruoyi.labdanger.service.ILabApplyPurchaseService;
import com.ruoyi.labdata.service.ILabFlowInstanceService;
import com.ruoyi.system.service.ISysUserService;

/**
 * 申请采购Service业务层处理
 * 
 * @author winner pan
 * @date 2022-03-09
 */
@Service
public class LabApplyPurchaseServiceImpl implements ILabApplyPurchaseService {
	@Autowired
	private LabApplyPurchaseMapper labApplyPurchaseMapper;
	@Autowired
	private ILabFlowInstanceService labFlowInstanceService;
	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private ILabApplyPurchaseDetailsService labApplyPurchaseDetailsService;

	/**
	 * 查询申请采购
	 * 
	 * @param instanceId
	 *            申请采购ID
	 * @return 申请采购
	 */
	@Override
	public LabApplyPurchase selectLabApplyPurchaseById(Long instanceId) {
		return labApplyPurchaseMapper.selectLabApplyPurchaseById(instanceId);
	}

	/**
	 * 查询申请采购列表
	 * 
	 * @param labApplyPurchase
	 *            申请采购
	 * @return 申请采购
	 */
	@Override
	public List<LabApplyPurchase> selectLabApplyPurchaseList(LabApplyPurchase labApplyPurchase) {
		SysUser loginUser = SecurityUtils.getLoginUser().getUser();
		labApplyPurchase.setCreateBy(loginUser.getUserName());
		return labApplyPurchaseMapper.selectLabApplyPurchaseList(labApplyPurchase);
	}

	/**
	 * 新增申请采购
	 * 
	 * @param labApplyPurchase
	 *            申请采购
	 * @return 结果
	 */
	@Override
	@Transactional
	public LabApplyPurchase insertLabApplyPurchase(LabApplyPurchase labApplyPurchase) {
		SysUser user = sysUserService.selectUserByUserName(labApplyPurchase.getCreateBy());
		labApplyPurchase.setCreateOrgId(user.getDeptId());
		labApplyPurchase.setApplyStatusOpt(Long.valueOf(1));
		labFlowInstanceService.insertLabFlowInstance(labApplyPurchase);
		labApplyPurchase.setCreateTime(new Date());
		labApplyPurchaseMapper.insertLabApplyPurchase(labApplyPurchase);
		//前端页面需要
		return labApplyPurchase;
	}

	/**
	 * 修改申请采购
	 * 
	 * @param labApplyPurchase
	 *            申请采购
	 * @return 结果
	 */
	@Override
	@Transactional
	public int updateLabApplyPurchase(LabApplyPurchase labApplyPurchase) {
		labApplyPurchaseMapper.updateLabApplyPurchase(labApplyPurchase);
		SysUser user = sysUserService.selectUserByUserName(labApplyPurchase.getCreateBy());
		labApplyPurchase.setCreateOrgId(user.getDeptId());
		return labFlowInstanceService.updateLabFlowInstance(labApplyPurchase);
	}
	
	@Override
	@Transactional
	public int submitLabApplyPurchase(LabApplyPurchase labApplyPurchase) {
		this.updateLabApplyPurchase(labApplyPurchase);
		return labFlowInstanceService.commitFlow(labApplyPurchase);
	}

	/**
	 * 批量删除申请采购
	 * 
	 * @param instanceIds
	 *            需要删除的申请采购ID
	 * @return 结果
	 */
	@Override
	public int deleteLabApplyPurchaseByIds(Long[] instanceIds) {
		return labApplyPurchaseMapper.deleteLabApplyPurchaseByIds(instanceIds);
	}

	/**
	 * 删除申请采购信息
	 * 
	 * @param instanceId
	 *            申请采购ID
	 * @return 结果
	 */
	@Override
	public int deleteLabApplyPurchaseById(Long instanceId) {
		LabApplyPurchase labApplyPurchase = this.selectLabApplyPurchaseById(instanceId);
		if(!labApplyPurchase.getFlowStatusOpt().equals("uncommitted")){
			throw new RuntimeException("采购单已经提交，不能删除!");
		}
		return labApplyPurchaseMapper.deleteLabApplyPurchaseById(instanceId);
	}
	@Transactional
	public int updateAcademyPurchase(LabApplyPurchase labApplyPurchase){
		int ret = this.labApplyPurchaseMapper.updateAcademyPurchase(labApplyPurchase);
		if(ret==0){
			throw new RuntimeException("该老师申请已经被学院采购选用过，请刷新页面检查。");
		}
		LabApplyPurchaseDetails labApplyPurchaseDetails = new LabApplyPurchaseDetails();
		labApplyPurchaseDetails.setInstanceId(labApplyPurchase.getInstanceId());
		labApplyPurchaseDetails.setPurchaseId(labApplyPurchase.getPurchaseId());
		labApplyPurchaseDetailsService.updatePuchaseId(labApplyPurchaseDetails);
		return ret;
	}

	public List<LabApplyPurchase> selectForAcademyPurchase(Long purchaseId){
		return this.labApplyPurchaseMapper.selectForAcademyPurchase(purchaseId);
	}
}
